package com.zl.service;

import com.zl.entity.Person;
import com.zl.entity.QuarkusUser;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;

import javax.enterprise.context.ApplicationScoped;
import javax.transaction.Transactional;
import javax.ws.rs.NotFoundException;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import java.util.Optional;

/**
 * @description Hibernate enforce  {@link <href="http://quarkus.io/guides/hibernate-orm-panache">}
 *
 * @date 2021-09-24 15:13
 *
 * @author Lizhong
 */

@ApplicationScoped
public class QUserService {

    public String say(String name) {
        return "PONG" + name;
    }

    @Transactional
    public void saveAll(List<QuarkusUser>quarkusUsers) {
        QuarkusUser.persist(quarkusUsers);
    }

    public List<PanacheEntityBase> list() {
       return QuarkusUser.listAll();
    }

    public void list2(Long personId) {
        // creating a person
        Person person = new Person();
        person.name = "Stef";
        person.birth = LocalDate.of(1910, Month.FEBRUARY, 1);
        person.status = Status.Alive;

// persist it
        person.persist();

// note that once persisted, you don't need to explicitly save your entity: all
// modifications are automatically persisted on transaction commit.

// check if it's persistent
        if(person.isPersistent()){
            // delete it
            person.delete();
        }

// getting a list of all Person entities
        List<Person> allPersons = Person.listAll();

// finding a specific person by ID
        person = Person.findById(personId);

// finding a specific person by ID via an Optional
        Optional<Person> optional = Person.findByIdOptional(personId);
        person = optional.orElseThrow(() -> new NotFoundException());

// finding all living persons
        List<Person> livingPersons = Person.list("status", Status.Alive);

// counting all persons
        long countAll = Person.count();

// counting all living persons
        long countAlive = Person.count("status", Status.Alive);

// delete all living persons
        Person.delete("status", Status.Alive);

// delete all persons
        Person.deleteAll();

// delete by id
        boolean deleted = Person.deleteById(personId);

// set the name of all living persons to 'Mortal'
        Person.update("name = 'Mortal' where status = ?1", Status.Alive);
    }


}
